import publicForm from "@/components/public/el-form.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    saveSpeciesData, //保存
    getSpeciesDataById, //详情
    getUnitname, //单位

} from "@/api/request.js";
export default {
    props: [
        // "dialogVisible",
        // "dialogTitle",
        // "statusNum",
        // "currentRow",//当前行
    ],
    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            btnStatus: [],
            dialogHeight: null,
            formObj: [],
            formData: {
                fId: "", //信息的fid
                fSort: "", //排序序号
                fDetecttype: "",
                fVarietyid: "", //种类ID
                fVarietyname: "", //种类名
                fMaterialid: "", //材料ID
                fMaterialname: "", //材料名

                fUnit: "", //材料单位
                fFrequency: "", //频率
                fStandard: "", //依据标准

                fStatus: true, //是否启用("1"启用"0"不启用),默认启用
                fNote: "", //描述说明
                userId: "", //当前登录账户ID
                userName: "", //当前登录账户名
                fFilingmaterial: "", //备案材料
                fFilinglevel: "" //备案等级
            },
            btnShow: true,
            currentDetail: null,
            fStatus: 1,
            primaryInfo: null,
            currentDetail: null,
            statusNum: null,
            currentRow: {}
        };
    },
    components: {
        publicForm,
        FormLyout
    },
    watch: {
        // dialogVisible: {
        //     handler(newVal) {
        //         if (newVal) {
        //             this.visible = newVal;
        //         }
        //     },
        //     immediate: true,
        // },
        // dialogTitle: {
        //     handler(newVal) {
        //         if (newVal) {
        //             this.title = newVal;
        //         }
        //     },
        //     immediate: true,
        // },
        // statusNum: {
        //     handler(newVal) {
        //         if (newVal) {
        //             console.log("statusNum", newVal);
        //             if (newVal == 1 || newVal == 2) {
        //                 this.formObj = formJs.formTypeObj
        //                 this.formData.fDetecttype = "自检"
        //             } else if (newVal == 4 || newVal == 5) {
        //                 this.formObj = formJs.formMaterialObj
        //                 this.formData.fDetecttype = ""

        //             }
        //             this.edit(newVal);
        //         }
        //     },
        //     immediate: true,
        // },
        // currentRow: {
        //     handler(newVal) {
        //         if (newVal) {
        //             this.currentDetail = newVal;
        //             this.formData = this.$options.data.call(this).formData;
        //             if (this.statusNum != 1 && this.statusNum != 4) {
        //                 this.getDetail({
        //                     fId: newVal.fId,
        //                 });
        //             }
        //         }
        //     },
        //     immediate: true,
        // },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        this.statusNum = this.primaryInfo.status;
        this.title = this.primaryInfo.dialogTitle;
        this.currentDetail = this.primaryInfo.data;

        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
        }, true);
        this.btnShow = true;
        if (this.statusNum == 1 || this.statusNum == 2) {
            this.formObj = formJs.formTypeObj
            this.formData.fDetecttype = "自检"
        } else if (this.statusNum == 4 || this.statusNum == 5) {
            this.formObj = formJs.formMaterialObj
            this.formData.fDetecttype = ""

        }
        if (this.statusNum == 1 || this.statusNum == 4) { //新增
            this.formData = this.$options.data.call(this).formData;
            this.btnShow = true
            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName; //当前登录人名称
            this.formObj.map((item) => {
                item.disabled = false;
            });
            if (this.statusNum == 4) {
                this.formData.fVarietyid = this.currentDetail.fVarietyid
                this.formData.fVarietyname = this.currentDetail.fVarietyname
                this.formObj[0].disabled = true;
            } else {
                this.formData.fVarietyid = ""
                this.formObj[0].disabled = false

                this.formData.fVarietyname = ""
            }
        } else if (this.statusNum == 2 || this.statusNum == 5) { //编辑
            // currentRow
            this.formData.fId = this.currentDetail.fId //按钮 
            this.formObj.map((item) => {
                item.disabled = false;
            });
            if (this.statusNum == 5) {
                this.formObj[0].disabled = true;
            } else {
                this.formObj[0].disabled = false
            }
            this.getDetail({
                fId: this.currentDetail.fId,
            });
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.getDetail({
                fId: this.currentDetail.fId,
            });
        }
        this.getHeight();
        this.getUnitname();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },

        closeDialog() {
            this.$emit("close");
        },
        formSwitchChange(val, index, prop) {

            if (prop == 'fStatus') {
                if (val == false) {
                    this.fStatus = 0
                } else if (val == true) {
                    this.fStatus = 1
                }
            }
        },
        selectChange(data, index, prop) {
            if (prop == "fFilingmaterial") {
                if (data == '是') {
                    this.formObj[3].rules[0].required = true;
                } else if (data == '否') {
                    this.formObj[3].rules[0].required = false;
                }
            }
        },
        //   edit(status)

        // 详情
        getDetail(obj) {
            this.loading = true;
            getSpeciesDataById(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data
                    this.formData = data
                    if (data.typ == 1) { //种类
                        this.formObj = formJs.formTypeObj;
                        if (this.statusNum == 2) {

                            if (data.fFilingmaterial == '是') {
                                this.formObj[3].rules[0].required = true;
                            } else if (data.fFilingmaterial == '否') {
                                this.formObj[3].rules[0].required = false;
                            }

                        }
                    } else if (data.typ == 2) { //材料
                        this.formObj = formJs.formMaterialObj;
                    }

                    if (data.fStatus == 0) {
                        this.formData.fStatus = false
                    } else if (data.fStatus == 1) {
                        this.formData.fStatus = true

                    }

                    if (this.statusNum == 7) {
                        this.btnShow = false;
                        this.formObj.map((item) => {
                            item.disabled = true;
                        });
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },

        yj_saveForm(status) {
            // 0连续新增1新增
            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName;
            if (this.$refs.publicForm.submitForm()) {

                this.saveForm(this.formData, status);

            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data, status) {
            this.loading = true;
            this.isLock = true
            this.formData.fStatus = this.fStatus == true ? 1 : 0
            saveSpeciesData(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$router.push({
                            path: "/materialDataManage/categoryManagement",
                        });
                        this.$message.success({
                            content: '新增成功',
                            duration: 1000
                        })
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error({
                            content: res.data,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },
        // 单位
        getUnitname() {
            formJs.formMaterialObj[3].options = [];
            getUnitname().then((res) => {
                if (res.errcode == 0) {
                    res.data.map(item => {
                        formJs.formMaterialObj[3].options.push({
                            value: item.fUnitname,
                            label: item.fUnitname
                        })
                    })
                }
            });
        },
    },
};